package com.cyclecoder.thread;

/**
 * @author by zhanglei
 * @Date 2021/8/9
 */
public class ThreadDemo extends Thread{
    private int ticket = 10;
    @Override
    public synchronized void start() {
        super.start();
    }

    @Override
    public void run() {
       while (true){
           synchronized (this) {
               if (ticket < 1) {
                   break;
               }
               try {
                   Thread.sleep(1000);
//                   Thread.yield();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread().getName() + ">>>" + ticket--);
           }
       }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadDemo t1 = new ThreadDemo();
        t1.start();
        System.out.println("finished");
    }
}
